﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sherwood.SignOn.Server.Models.ViewModels;
using System.Net.Mail;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.SignOn.Server.Models.Services;

namespace Sherwood.SignOn.Server.Controllers
{
    public class SupportController : ControllerBase
    {
        [HttpGet]
        [ValidateInput(false)] 
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)] 
        public ActionResult Index(SupportViewModel model)
        {
            if (ModelState.IsValid)
            {
                string supportEmailText = string.Format(@"<b>Name:</b> {0}<br/>
<b>Email:</b> {1}<br/>
<b>Username:</b> {2}<br/>
<b>Country:</b> {3}<br/>
<b>Location:</b> {4}<br/>
<br/>
{5}
<br/><br/>",model.Name, model.Email, model.Username != null ? model.Username : "", model.Country, model.Town, model.Message.Replace("\n","<br/>"));

                MailAddress toAddress = new MailAddress(Config.Settings.SupportEmailAddress);
                SmtpClient emailClient = new SmtpClient();
                MailMessage message = new MailMessage();
                message.From = new MailAddress(model.Email, model.Name);
                message.Sender = new MailAddress(model.Email, model.Name);
                message.To.Add(toAddress);
                message.IsBodyHtml = true;
                message.Subject = model.Subject;
                message.Body = supportEmailText;
                emailClient.Send(message);

                return View("SupportConfirmation", model);
            }
            else
            {
                return View(model);
            }
        }



    }
}
